import yargs from 'yargs';
import { choicesPackageManages, filterPackageManager } from 'create-ts-next';
import { WebpackNextProjectCreator } from './WebpackNextProjectCreator';

export async function createWebpackProject() {
  const argv = yargs(process.argv.slice(2))
    .usage('$0 <name>', 'Specify the project name')
    .options({
      debug            : {
        alias  : 'd',
        type   : 'boolean',
        default: false,
      },
      mock             : {
        alias  : 'M',
        type   : 'boolean',
        default: false,
      },
      install          : {
        alias  : 'i',
        type   : 'boolean',
        default: true,
      },
      'package-manager': {
        alias  : 'p',
        type   : 'string',
        default: 'npm',
        choices: choicesPackageManages(),
      },
      'react'          : {
        alias  : 'r',
        type   : 'string',
        default: '18',
        choices: ['17', '18'],
      },
      'vite'           : {
        type   : 'boolean',
        default: false,
      },
    })
    .parseSync();

  const { name, debug, mock, install, packageManager, react, vite } = argv;

  if (name == null) {
    throw new Error('unspecified project name');
  }

  await (new WebpackNextProjectCreator({
    name          : name + '',
    debug,
    mock,
    install,
    packageManager: filterPackageManager(packageManager),
    react,
    vite,
  })).startUp();
}
